<?php
class Sign_model extends CI_Model{
	function __construct(){
		parent::__construct();
	}

	public function signup($email,$pw,$name){
		
		if($this->is_validate_email($email)){
			return array('status'=>false,'msg'=>'该邮箱已被注册！');
		}
		$data['email'] = $email;
		$data['password'] = md5($pw);
		$data['name']=$name;
		$data['signup_time'] = date('Y-m-d H:i:s');
		if($this->db->insert('user',$data)){
			return array('status'=>true);
		}
	}

	public function is_validate_email($email){
	
		$this->db->where('email',$email);
		return $this->db->get('user')->num_rows();
	}

	public function signin($email,$pw){
		if(!$this->is_validate_email($email)){
			return array('status'=>false,'msg'=>'该邮箱还未注册');
		}

		$this->db->where('email',$email);
		$this->db->where('password',md5($pw));
		$query = $this->db->get('user');
		if($query->num_rows() > 0){
			$user = $query->row();
			$this->update_session($user);
			return array('status'=>true);
		}else{
			return array('status'=>false,'msg'=>'邮箱和密码不匹配，请重新输入！');
		}
	}

	public function update_session($user){
		$this->session->set_userdata('uid',$user->id);
		$this->session->set_userdata('uname',$user->name);
		$this->session->set_userdata('level',$user->level);
	}
}